/**
 * Created by smp on 2017/3/15.
 */
var isSearchBarExpand = false;
var index;

var modelPath=ROOT_PATH+'/permission/dictionary/';
function doSearch(value, name) {
    alert('You input: ' + value + '(' + name + ')');
}
function toggleSearch() {
    var $mainLayout = $('#main_layout');
    var expandHeight = 200;
    var collapseHeight = 50;
    if (!isSearchBarExpand){
        $mainLayout.layout('panel','north').panel('resize',{height:expandHeight});
        $mainLayout.layout('resize');
        isSearchBarExpand = true;
    }
    else {
        $mainLayout.layout('panel','north').panel('resize',{height:collapseHeight});
        $mainLayout.layout('resize');
        isSearchBarExpand = false;
    }
}
/**
 * 关闭layer
 */
function closeLayer() {
    layer.close(index);
}
/**
 * 使用layer 打开一个页面
 * @param title
 * @param content
 */
function openLayer(title,content) {

    index=layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        area: ['600px', '600px'],
        content: modelPath+content
    });
}
function result(data) {
    if(data==1){
        layer.msg("保存成功");
        closeLayer();
    }else{
        layer.alert("保存失败");
    }
}
$(function () {
    //初始化加载datagrid
    $('#dictionary').datagrid({
        method: 'post',
        url: ROOT_PATH + '/permission/dictionary/list.shtml',
        queryParams: {
            sort: 'sort',
            order: 'asc'
        },
       // idField: 'id',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect:true,
        sortOrder: true,
        striped: true,
        fit: true,
        columns: [[
            {field: 'id', width: 80, checkbox: true},
            {field: 'code', title: '字典标识', width: 100,  sortable: true},
            {field: 'name', title: '字典名称', width: 80,  sortable: true},
            {
                field: 'typeId',
                title: '字典类型',
                width: 80,

                sortable: true,
                formatter: function (value, row, index) {
                    if (row.dictionaryType&&row.dictionaryType.name) {
                        return row.dictionaryType.name;
                    } else {
                        return '';
                    }
                }
            },
            {
                field: 'readonly',
                title: '是否可编辑',
                width: 60,

                sortable: true,
                formatter: function (value, row, index) {
                    if (value == 1) {
                        return '是';
                    } else {
                        return '否';
                    }
                }
            },
            {field: 'remarks', title: '备注', width: 100, }
        ]],
        toolbar: '#toolbar'
    });

    //添加按钮事件
    $('#btn_dictionary_add').on('click',function () {

        var title='添加字典';
        var content='addDictionary.shtml';
        openLayer(title,content);
    });
    //编辑按钮事件
    $('#btn_dictionary_edit').on('click',function () {

        var checkedRows=$('#dictionary').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要编辑的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        var readonly=checkedRows[0].readonly;
        if(readonly==0){
            layer.alert("不可编辑！");
            return
        }
        var id=checkedRows[0].id;
        var title='编辑字典';
        var content='editDictionary.shtml?id='+id;
        openLayer(title,content);
    });
    //删除按钮事件
    $('#btn_dictionary_delete').on('click',function () {

        var checkedRows=$('#dictionary').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要删除的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        var readonly=checkedRows[0].readonly;
        if(readonly==0){
            layer.alert("不可删除！");
            return
        }
        var id=checkedRows[0].id;
        layer.confirm('是否确定删除当前选中数据？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                type: "POST",
                url: ROOT_PATH+'/permission/dictionary/deleteDictionary.shtml',
                data: {id:id,delState:1},
                dataType: "json",
                success: function(result){
                    if (result.success) {
                        $('#dictionary').datagrid('clearSelections').datagrid('reload');
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 1000
                        });
                    }
                    else {
                        layer.msg('删除失败', {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            });
        });

    });
    $.ajax({
        url:ROOT_PATH + '/permission/dictionaryType/listAll.shtml',
        type:"post",
        dataType:"json",
        success:function(data){
            $('#ccc').combobox({
                data:data,
                valueField:'id',
                textField:'name',
                value:null
                /*onLoadSuccess: function () { //加载完成后,设置选中第一项
                    var val = $(this).combobox("getData");
                    for (var item in val[0]) {
                        if (item == "id") {
                            $(this).combobox("select", val[0][item]);
                        }
                    }
                }*/
            });
        }
    });
    /**
     * 查询按钮
     */
    $('#selectDictionary').on('click',function(){
        var name=$('#name').val();
        var typeId=$('#ccc').val();
        $('#dictionary').datagrid('load', {name:name,typeId:typeId,sort: 'sort', order: 'asc'});
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
    });
});